/*
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Description: 动态数码管实验
 * @Date: 2021-10-27 09:42:25
 * @LastEditTime: 2021-11-08 08:59:11
 */
#include "gsmg.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "commom.h"

//将 P0 口定义为 SMG_PORT 数码管端口
#define SMG_PORT P0

uint8 gsmg_codes[17] = {
    0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f,
    0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00};

//定义数码管位选信号控制脚
sbit LSA = const_P2 ^ 2;
sbit LSB = const_P2 ^ 3;
sbit LSC = const_P2 ^ 4;


/**
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Date: 2021-10-27 11:50:13
 * @brief 设置某位数码管显示
 * @param {uint8} position
 * @return {*}
 */
static void set_position(uint8 position)
{
    switch (position) //位选
    {
    case 7:
        LSC = 1, LSB = 1, LSA = 1;
        break;
    case 6:
        LSC = 1, LSB = 1, LSA = 0;
        break;
    case 5:
        LSC = 1, LSB = 0, LSA = 1;
        break;
    case 4:
        LSC = 1, LSB = 0, LSA = 0;
        break;
    case 3:
        LSC = 0, LSB = 1, LSA = 1;
        break;
    case 2:
        LSC = 0, LSB = 1, LSA = 0;
        break;
    case 1:
        LSC = 0, LSB = 0, LSA = 1;
        break;
    case 0:
    default:
        LSC = 0, LSB = 0, LSA = 0;
        break;
    }
}

/**
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Date: 2021-10-27 09:46:09
 * @brief 动态数码管流水
 * @param {*}
 * @return {*}
 */
// void smg_water()
// {
//     uint8 i;
//     for (i = 0; i < 17; i++)
//     {
//         SMG_PORT = gsmg_codes[i];
//         delay_1ms(500);
//     }
// }

/**
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Date: 2021-10-27 18:43:45
 * @brief 使 opsition 上的数码管显示数字 number
 * @param {uint8} number
 * @param {uint8} position
 * @return {*}
 */
void smg_item_show_number(GsmgCode number, uint8 position)
{
    set_position(position);
    SMG_PORT = gsmg_codes[number];
}

/**
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Date: 2021-10-27 09:46:09
 * @brief 动态数码管流水
 * @param {*}
 * @return {*}
 */
// void smg_display()
// {
//     uint8 i = 0;
//     for (i = 0; i < 16; i++)
//     {
//         smg_item_show_number((i % 16), i % 8);
//         delay_1ms(1000);
//         SMG_PORT = 0x00;
//     }
// }

/**
 * @Author: 玄绮 <shijiahansi@qq.com>
 * @Date: 2021-10-27 18:46:37
 * @brief 使数码管显示数字 number
 * @param {uint32} number
 * @return {*}
 */
void smg_show_number(uint32 number)
{
    GsmgCode nums[8] = {0};
    uint8 i = 0, chu = 10;
    for (i = 0; i < 8; i++)
    {
        nums[i] = gsmg_code_null;
    }

    for (i = 0; number > 0; i++)
    {
        nums[i] = number % 10;
        number /= 10;
    }

    for (i = 0; i < 8; i++)
    {
        smg_item_show_number(nums[i], i);
        delay_10us(50);
        SMG_PORT = 0x00; //消影
    }
}